package aom.ju.ss.view.user.shop.management.goods.addGoods

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ActivityIndustryCateslpBinding
import aom.ju.ss.databinding.ClassOneClassBinding
import aom.ju.ss.databinding.ClassTwosClassBinding
import aom.ju.ss.mode.*
import aom.ju.ss.utils.Fin
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.dialog.TitleDialog
import aom.ju.ss.view.shua.ClassFragment
import com.google.gson.Gson

/**
 * 说明: 经营类目
 */
class IndustryClassActivityAddGoods : BaseActivity() {
    private lateinit var binding: ActivityIndustryCateslpBinding
    private lateinit var adapterClassOne: PAdapter
    private lateinit var adapterClassThree: PAdapter
    private var classListOne: MutableList<ClassModeDataOne> = arrayListOf()
    private var ckp = 0
    private var classListThree: MutableList<ClassModeDataInfoThree> = arrayListOf()
    private var cateList: MutableList<ClassModeDataOne> = arrayListOf()
    private var cktwoP = -1
    private var ckdata: ClassModeDataInfoThree? = null

    var idone = ""
    var idTwo = ""
    override fun init() {
        binding = viewBinding as ActivityIndustryCateslpBinding

        binding.title.r_title.setOnClickListener {
            cateList.clear()
            for (a in classListOne) {
                if (a.ck) {
                    cateList.add(a)
                }
            }
            if (ckdata == null) {
                TitleDialog.showMsg(this, "请选择经营类目")
                return@setOnClickListener
            }


            val lists = arrayListOf<ClassModeDataInfoThree>()
            val intent = Intent()

            val c = ClassModeDataInfoThree("", "", "", "", "", "", "", "", false)
            c.title = ckdata!!.title
            c.id = ckdata!!.id
            c.idtwo = idTwo
            c.idone = idone
            lists.add(c)

            intent.putParcelableArrayListExtra("data", lists)

            setResult(RESULT_OK, intent)
            finish()
        }
        binding.small.setEnableLoadMore(false)
        binding.classTwo.small.isEnabled = false
        binding.sx.setOnClickListener {
            binding.small.autoRefresh()
        }
        binding.small.setOnRefreshListener {
            data
        }
        data
        adapterClassOne = PAdapter(
            classListOne, R.layout.class_one_class
        ) { b, position ->
            val ib = b as ClassOneClassBinding

            ib.ck = classListOne[position].ck
            ib.nameStr = classListOne[position].title

            ib.nameTv.setOnClickListener {
                idone = classListOne[position].id

                classListOne[position].ck = !classListOne[position].ck
                classListOne[ckp].ck = false
                adapterClassOne.notifyItemChanged(ckp)
                adapterClassOne.notifyItemChanged(position)

                ckp = position
                for (a in classListThree) {
                    a.ck = false
                }
                classListThree.clear()

                classListOne[position].child?.let { ls ->
                    if (classListOne[position].ck) {//如果选中了 一个大分类 给小分类添加数据
                        for (d in ls) {
                            d.child?.let { asl ->
                                classListThree.addAll(asl)
                            }
                        }

                    }
                }

                adapterClassThree.notifyDataSetChanged()
                binding.classTwo.noData.VG(classListThree.size == 0)

            }

        }
        binding.classOne.layoutManager = LinearLayoutManager(this)
        binding.classOne.adapter = adapterClassOne

        adapterClassThree = PAdapter(
            classListThree, R.layout.class_twos_class
        ) { b, position ->
            val ib = b as ClassTwosClassBinding
            ib.name.text = classListThree[position].title
            ib.ck = classListThree[position].ck
            ib.rootView.setOnClickListener {
                idTwo = classListThree[position].pid
                classListThree[position].ck = !classListThree[position].ck

                if (cktwoP != -1) {
                    classListThree[cktwoP].ck = false
                }
                ckdata = classListThree[position]

                adapterClassThree.notifyItemChanged(position)
                adapterClassThree.notifyItemChanged(cktwoP)
                cktwoP = position

            }
        }
        binding.classTwo.mRecycler.layoutManager = GridLayoutManager(this, 3)
        binding.classTwo.mRecycler.adapter = adapterClassThree
    }

    override val lay: Int = R.layout.activity_industry_cateslp


    val data: Unit
        get() {
            OKHttpUtils(activity)
                .SetApiUrl(LP_API.get_tree)
                .POST(this)
        }


    companion object {
        fun newInstance(): ClassFragment {
            val fragment = ClassFragment()
            val bd = Bundle()
            fragment.arguments = bd
            return fragment
        }
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ClassMode::class.java)
        binding.small.Fin()
        classListOne.clear()

        classListThree.clear()
        mode.data?.let { it ->
            if (it.size > 1) {
                classListOne.addAll(it)
                idone = it[0].id
                if (classListOne.size >= 1) {
                    classListOne[0].ck = true
                    classListOne[0].ckline = true

                    classListOne[0].child?.let { ls ->
                        if (ls.isNotEmpty()) {
                            for (a in ls) {
                                a.child?.let { als ->
                                    classListThree.addAll(als)
                                }
                            }
                        }
                    }
                }
            }
        }

        binding.classTwo.noData.VG(classListThree.size == 0)
        binding.sx.VG(classListOne.size == 0)
        adapterClassOne.notifyDataSetChanged()
        adapterClassThree.notifyDataSetChanged()
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        binding.small.Fin()
        e.show
    }
}